package com.koala;


import cn.hutool.core.codec.Base64;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.koala.entity.Car;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.junit.jupiter.api.Test;
import org.springframework.util.CollectionUtils;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDate;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 开始类
 *
 * @author yantingrui
 * @date 2021/12/1   22:44
 */
@Slf4j
public class Start {
    private static String key = "1234567890000000";

    private static String ivStr = "1234567890000000";

    private static String src = "123";


    /**
     *
     */
    @Test
    public void test53() {
        ArrayList<Car> l1 = Lists.newArrayList();
        ArrayList<Car> l2 = Lists.newArrayList();
        l1.add(new Car("A", "1"));
        l1.add(new Car("B", "2"));
        ImmutableMap<String, Car> res1 = Maps.uniqueIndex(l1, Car::getBrand);
        ImmutableMap<String, Car> res2 = Maps.uniqueIndex(l2, Car::getBrand);
        Sets.SetView<String> difference = Sets.difference(res2.keySet(), res1.keySet());
        System.out.println(difference);
    }

    /**
     *
     */
    @Test
    public void test51() {
        LocalDate now = LocalDate.of(2022, 9, 8);
        LocalDate after = LocalDate.of(2021, 8, 10);
        int count = 0;
        while (after.isBefore(now)) {
            System.out.println(after);
            after = after.plusMonths(1);
            count++;
        }
        System.out.println(count);
    }

    /**
     *
     */
    @Test
    public void test37() throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        byte[] raw = key.getBytes();
        byte[] encrypted1 = null;
        byte[] encrypted2 = null;
        byte[] encrypted = null;
        SecretKeySpec secretKeySpec = new SecretKeySpec(raw, "AES");
        Cipher c1 = Cipher.getInstance("AES/ECB/NoPadding"); // Noncompliant
        Cipher c2 = Cipher.getInstance("AES/CBC/PKCS5Padding"); // Noncompliant
        Cipher c = Cipher.getInstance("AES/GCM/NoPadding");
        IvParameterSpec iv = new IvParameterSpec(ivStr.getBytes());
        //c1.init(Cipher.ENCRYPT_MODE, secretKeySpec, iv);
        //encrypted1 = c1.doFinal(src.getBytes());
        //org.apache.commons.codec.binary.Base64.encodeBase64String(encrypted1);

        c2.init(Cipher.ENCRYPT_MODE, secretKeySpec, iv);
        encrypted2 = c2.doFinal(src.getBytes());
        org.apache.commons.codec.binary.Base64.encodeBase64String(encrypted2);

        c.init(Cipher.ENCRYPT_MODE, secretKeySpec, iv);
        encrypted = c.doFinal(src.getBytes());
        org.apache.commons.codec.binary.Base64.encodeBase64String(encrypted);

        System.out.println(encrypted1);
        System.out.println(encrypted2);
        System.out.println(encrypted);
    }

    /**
     * 集合包含另外一个集合
     * 子集 为true
     */
    @Test
    public void test262() {
        String appKey = RandomUtil.randomNumbers(10);
        String secret = UUID.randomUUID().toString();
        String clientSecret = SecureUtil.md5(secret);
        String clientSecretStr = Base64.encode(appKey + ":" + secret);
        System.out.println("appKey:" + appKey);
        System.out.println("secret:" + secret);
        System.out.println("clientSecret:" + clientSecret);
        System.out.println("clientSecretStr:" + clientSecretStr);
    }

    /**
     *
     */
    @Test
    public void test63() {
        List<String> list1 = Arrays.asList("A", "B", "C");
        String join = Joiner.on(",").join(list1);
        System.out.println(join);
    }

    /**
     *
     */
    @Test
    public void test62() {
        String s = "A,B,C";
        List<String> list = Splitter.on(",").splitToList(s);

    }

    /**
     *
     */
    /**
     *
     */
    @Test
    public void test66() {
        List<Integer> l1 = null;
        if (CollectionUtils.isEmpty(l1)) {
            l1 = Lists.newArrayList();
            l1.add(1);
            l1.add(2);
            l1.add(3);
        }
    }

    /**
     *
     */
    @Test
    public void test40() {
        String appKey = RandomUtil.randomNumbers(10);    // 给他们  数据库自己保存
        String secret = UUID.randomUUID().toString();    // 给他们
        String clientSecret = SecureUtil.md5(secret);    // 数据库自己保存
        String clientSecretStr = Base64.encode(appKey + ":" + secret);   // 数据库自己保存
        System.out.println("appKey:" + appKey);
        System.out.println("secret:" + secret);
        System.out.println("clientSecret:" + clientSecret);
        System.out.println("clientSecretStr:" + clientSecretStr);
    }

    private static void deal(List<String> phones) {
        for (String item : phones) {
            log.info("手机号为{}  {}", item, "座机");
            //System.out.println(item);
        }
    }

    /**
     *
     */
    @Test
    public void test410() {
        String s = "432524198202275210";
        boolean validCard18 = IdcardUtil.isValidCard18(s);
        System.out.println(validCard18);

    }

    /**
     * 正则判断手机号
     *
     * @param phone
     * @return
     */
    public static boolean isPhone(String phone) {
        String regex = "^((13[0-9])|(14[5,6,7,9])|(15[^4])|(16[5,6])|(17[0-9])|(18[0-9])|(19[1,3,5,8,9]))\\d{8}$";
        if (phone.length() != 11) {
            return false;
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();
            return isMatch;
        }
    }


    @Test
    public void test2() {
        System.out.println("hello world");
    }

    @Test
    public void test3() {

    }

    @Test
    public void test4() throws IllegalAccessException {
        HashMap<String, String> hashMap = Maps.newHashMap();
        hashMap.put("1","A");
        hashMap.put("2","B");
        Map<String, String> res ;
        res = ImmutableMap.copyOf(hashMap);
        System.out.println(JSON.toJSONString(res));
    }

    @Test
    public void test5(){
        ArrayList<String> strings = new ArrayList<>();
        strings.add("A");
        strings.add("B");

    }

    @Test
    public void test6(){
        String check = "（删除）";
        String s = "武汉（删除）";
        String s2 = "科学";
        String s3 = "科学（已删除）";
        boolean b1 = s.contains(check);
        boolean b2 = s2.contains(check);
        boolean b3 = s3.contains(check);
        System.out.println(b1);
        System.out.println(b2);
        System.out.println(b3);

    }

    @Test
    public void test7(){
        List<Integer> list2  = Arrays.asList(12, 3, 4, 5, 4);
        list2.stream().filter(i -> i % 2 == 0)
                .distinct()
                .forEach(System.out::println);
    }



}
